﻿using HuaHan.WorkTicket.Domain;
using HuaHan.WorkTicket.ServiceContracts;
using KendoGridBinder;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WorkTicket.Application.Implementation;
using WorkTicket.Website.Common;

namespace WorkTicket.Website.Controllers
{
    public class DepartmentController : Controller
    {
        protected IDepartmentService departmentService = new DepartmentServiceImpl();
        //
        // GET: /Department/

        public ActionResult Index()
        {
            return View();
        }

        public JsonResult GetAllItems(KendoGridRequest request)
        {
            List<Department> result = departmentService.GetAll();
            foreach (var item in result)
            {
                Department parentDepartment = departmentService.GetItemByID(item.ParentID.ToString());
                if (parentDepartment!=null)
                    item.ParentDepartmentName = parentDepartment.Name;
            }
            return Json(new { data = result, total = result.Count },JsonRequestBehavior.AllowGet); 
        }
        
        [HttpPost]
        public JsonResult CheckNameExisted()
        {
            string name = Request["Name"];
            string id = Request["ID"];
            JsonResult result = new JsonResult();
            result.Data = departmentService.CheckNameExisted(name, Guid.Parse(id));
            return result;
        }

        //
        // GET: /Department/Create
        [HttpGet]
        public ActionResult Create()
        {
            Department department = new Department();
            this.GetParentDeptList();
            return View(department);
        }

        //
        // POST: /Department/Create

        [HttpPost]
        public ActionResult CreateAndEdit(Department model)
        {
            try
            {
                // TODO: Add insert logic here
                if (Request.IsAjaxRequest())
                {
                    if (model.DepartmentID.ToString() == PublicHelper.EmptyGuid.ToString()) //add new entity
                    {
                        model.DepartmentID = Guid.NewGuid();
                        departmentService.Add(model);
                    }
                    else  //update entity
                    {
                        if (ModelState.IsValid)
                        {
                            departmentService.Update(model);
                        }
                    }
                    
                }
                return Json(new { data = true, message = "OK" },JsonRequestBehavior.AllowGet); 
            }
            catch
            {
                return Json(new { data = false, message = "Failed" }, JsonRequestBehavior.AllowGet); 
            }
        }

        //
        // GET: /Department/Edit/5
        [HttpGet]
        public ActionResult Edit(string id)
        {
            this.GetParentDeptList();
            var department= departmentService.GetItemByID(id);
            return View("Create",department);
        }

        //
        // GET: /Department/Delete/5
        public ActionResult Delete(string id)
        {
            try
            {
                //该部门下面存在子部门不能被删除
                if (departmentService.GetAll().Where(c => c.ParentID.ToString() == id).Count() > 0) 
                {
                    return Json(new { message = "该部门下面存在子部门不能被删除!" }, JsonRequestBehavior.AllowGet);
                }
                
                departmentService.Delete(id);
                return Json(new { message = "ok" },JsonRequestBehavior.AllowGet);
            }
            catch
            {
                return Json(new { message = "no" }, JsonRequestBehavior.AllowGet);
            }
        }

        private void GetParentDeptList()
        {
            ViewData["DeptList"] = from a in departmentService.GetAll()
                                   select new SelectListItem
                                   {
                                       Text = a.Name,
                                       Value = a.DepartmentID.ToString()
                                   };

        }

        public ActionResult OrgTree()
        {
            List<Department> result = new List<Department>();
            string id = Request["DepartmentID"];
            if (id == null)
            {
                id = PublicHelper.EmptyGuid.ToString();
            }
            result = departmentService.GetAll().Where(c => c.ParentID == Guid.Parse(id)).ToList();
            return Json(result, JsonRequestBehavior.AllowGet); 
        }

    }
}
